/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.editor.DkSimpleBeanEditorDriver;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxt.client.ui.component.DkInlineLabel;
import com.daikit.daikit4gxt.client.ui.edit.ValueChangeToNullOrSelectionChangeEvent;
import com.daikit.daikit4gxt.client.ui.edit.ValueChangeToNullOrSelectionChangeEvent.ValueChangeToNullOrSelectionChangeHandler;
import com.daikit.daikit4gxt.client.ui.popup.DkLocalEditCreatePopup;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.editor.SupplierDetailEditor;
import com.daikit.daikit4gxtsample.shared.bean.SupplierData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.daikit.daikit4gxtsample.shared.bean.WarehouseData;
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.validator.EmptyValidator;


/**
 * Popup for editing {@link SupplierDetailData}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class SupplierDetailEditCreatePopup extends DkLocalEditCreatePopup<SupplierDetailData, SupplierDetailEditor>
{

	private static SupplierDetailEditCreatePopup instance = null;

	/**
	 * @return the unique instance of this popup
	 */
	public static final SupplierDetailEditCreatePopup get()
	{
		if (instance == null)
		{
			instance = new SupplierDetailEditCreatePopup();
		}
		return instance;
	}

	private final FieldLabel warehouseFieldLabel;

	protected SupplierDetailEditCreatePopup()
	{
		super(new SupplierDetailEditor());

		addField(new FieldLabel(getEditor().id, Main.gwtSampleI18n().supplier_detail_id()));
		addField(new FieldLabel(getEditor().name, Main.gwtSampleI18n().supplier_detail_name()));
		addField(new FieldLabel(getEditor().supplier, Main.gwtSampleI18n().supplier_detail_supplier()));
		addField(new FieldLabel(getEditor().availability.startDate, Main.gwtSampleI18n().supplier_availability_startDate()));
		addField(new FieldLabel(getEditor().availability.endDate, Main.gwtSampleI18n().supplier_availability_endDate()));
		addField(new DkInlineLabel("Sample inline label"));
		warehouseFieldLabel = new FieldLabel(getEditor().warehouse, Main.gwtSampleI18n().supplier_detail_warehouse());
		addField(warehouseFieldLabel);

		getEditor().supplier
				.addValueChangeToNullOrSelectionChangeHandler(new ValueChangeToNullOrSelectionChangeHandler<SupplierData>()
				{
					@Override
					public void onSelect(final ValueChangeToNullOrSelectionChangeEvent<SupplierData> event)
					{
						if (event.getNewValue() != null)
						{
							warehouseFieldLabel.setVisible(true);
						}
					}
				});
		getEditor().name.addValidator(new EmptyValidator<String>());
		getEditor().supplier.addValidator(new EmptyValidator<SupplierData>());
		getEditor().warehouse.addValidator(new EmptyValidator<WarehouseData>());

		getEditor().id.setEnabled(false);
		setShowAfterSaveConfirmation(false);
	}

	interface ThisEditorDriver extends DkSimpleBeanEditorDriver<SupplierDetailData, SupplierDetailEditor>
	{
	}

	private final ThisEditorDriver driver = GWT.create(ThisEditorDriver.class);

	@Override
	protected SupplierDetailData createBeanInstance()
	{
		return new SupplierDetailData();
	}

	@Override
	protected void onModelSaved(final SupplierDetailData updatedModel)
	{
		DkLogger.debug(getClass(), "Supplier detail saved locally");
	}

	@Override
	protected DkSimpleBeanEditorDriver<SupplierDetailData, SupplierDetailEditor> getDriver()
	{
		return driver;
	}

	@Override
	protected String getLabelPopupTitle()
	{
		return "Edit supplier Detail";
	}

	@Override
	protected String getLabelActionSaveLoadingLabel()
	{
		return "Saving Supplier Detail...";
	}

	@Override
	public void invalidateUi()
	{
		warehouseFieldLabel.setVisible(!isCreation());

	}

	@Override
	protected String getLabelPopupSaveOkMessageBoxTitle()
	{
		return null;
	}

	@Override
	protected String getLabelPopupSaveOkMessageBoxMessage()
	{
		return null;
	}

}
